package ${package.Controller};

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

<#if "true" == cfg.createAdd>
import com.han.fu.api.business.model.ao.${entity}AddAo;
</#if>
<#if "true" == cfg.createUpdate>
import com.han.fu.api.business.model.ao.${entity}UpdateAo;
</#if>
<#if "true" == cfg.createPage>
import com.han.fu.api.business.model.ao.${entity}PageAo;
import com.han.fu.api.business.model.vo.${entity}Vo;
</#if>
<#if "true" == cfg.createDetail>
import com.han.fu.api.business.model.vo.${entity}InfoVo;
</#if>
<#if "true" == cfg.createStats>
import com.han.fu.api.business.model.ao.${entity}StatsAo;
import com.han.fu.api.business.model.vo.${entity}StatsVo;
</#if>
<#if "true" == cfg.createDelete>
import com.han.fu.api.business.model.ao.${entity}DeleteAo;
</#if>
import ${package.Service}.${table.serviceName};
import org.apache.tomcat.util.http.fileupload.IOUtils;
import com.han.fu.api.common.model.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;

import java.util.List;

/**
 * ${table.comment!}接口
 *
 * @author: ${author}
 * @date: ${date}
 */
@RestController
public class ${table.controllerName} {

    @Autowired
    I${entity}Service i${entity}Service;
<#if "true" == cfg.createPage>

    /**
     * ${table.comment!}分页查询
     *
     * @param ao 分页查询条件
     * @return Page<${entity}Vo> 分页数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/${controllerMappingHyphen?replace("-","/")}/page")
    public Page<${entity}Vo> query${entity}Page(@Validated @RequestBody ${entity}PageAo ao) {
        return i${entity}Service.query${entity}Page(ao);
    }
</#if>
<#if "true" == cfg.createDetail>

    /**
     * ${table.comment!}明细查询
     *
     * @param id 主键
     * @return ${entity}InfoVo 明细
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @GetMapping("/${controllerMappingHyphen?replace("-","/")}/detail")
    public ${entity}InfoVo query${entity}Detail(@NotNull(message = "ID不能为空") @RequestParam String id) {
        return i${entity}Service.query${entity}Detail(id);
    }
</#if>
<#if "true" == cfg.createAdd>

    /**
     * ${table.comment!}新增数据
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/${controllerMappingHyphen?replace("-","/")}/add")
    public void add${entity}(@Validated @RequestBody ${entity}AddAo ao) {
        i${entity}Service.add${entity}(ao);
    }
</#if>
<#if "true" == cfg.createUpdate>

    /**
     * ${table.comment!}修改数据
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/${controllerMappingHyphen?replace("-","/")}/update")
    public void update${entity}(@Validated @RequestBody ${entity}UpdateAo ao) {
        i${entity}Service.update${entity}(ao);
    }
</#if>
<#if "true" == cfg.createDelete>

    /**
     * ${table.comment!}删除数据
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/${controllerMappingHyphen?replace("-","/")}/delete")
    public void delete${entity}(@Validated @RequestBody ${entity}DeleteAo ao) {
        i${entity}Service.delete${entity}(ao);
    }
</#if>
<#if "true" == cfg.createExport>

   /**
     * ${table.comment!}导出
     *
     * @param ao 分页查询条件
     * @param response 响应
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @GetMapping("/${controllerMappingHyphen?replace("-","/")}/export")
    public void export${entity}(${entity}PageAo ao, HttpServletResponse response) throws IOException {
        StringBuilder fileName = new StringBuilder();
        fileName.append("${table.comment!}_").append(LocalDate.now()).append(".xlsx");
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName.toString(), "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        i${entity}Service.export${entity}(ao, outputStream);
        IOUtils.closeQuietly(outputStream);
    }
</#if>
<#if "true" == cfg.createStats>

    /**
     * ${table.comment!}统计查询
     *
     * @param ao 查询条件
     * @return List<${entity}StatsVo> 统计数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/${controllerMappingHyphen?replace("-","/")}/stats")
    public List<${entity}StatsVo> query${entity}Stats(@Validated @RequestBody ${entity}StatsAo ao) {
        return i${entity}Service.query${entity}Stats(ao);
    }
</#if>

}